package lib.pacificapp.app.action;
/**lib.pacificapp.app.action.CameraAction*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TResult; 
 
import android.annotation.SuppressLint;
import android.content.Intent; 
import android.content.res.Configuration;
import android.graphics.Bitmap; 
import android.net.Uri;
import android.os.Environment; 
import android.util.Base64; 
import android.view.View; 
 
public class CameraAction extends TakePhotoActivity{
	 
   public static final int CANCEL=0x1001;
   public static final int SUCCESS=0x1002;
   public static final int ERROR=0x1003;
 
	@Override
	public void takeCancel() {
		// TODO Auto-generated method stub
		super.takeCancel();
		Intent mIntent = new Intent();   
		 setResult(CANCEL, mIntent); 
		 
	}
	 
	 @Override
	public void takeFail(TResult result, String msg) {
		// TODO Auto-generated method stub
		super.takeFail(result, msg);
		Intent mIntent = new Intent();   
		 setResult(ERROR, mIntent); 
	}
	 @Override
	public void takeSuccess(TResult result) {
		// TODO Auto-generated method stub
		super.takeSuccess(result); 
		 Intent mIntent = new Intent(); 
//		 if(result.getImages().size()>0){
//             mIntent.putExtra("src", result.getImages().get(0).getCompressPath());
//		 }else{
//		     mIntent.putExtra("src", result.getImage().getCompressPath());
//		   }
		  setResult(SUCCESS, mIntent); 
		  finish();
	}
	
	@Override
	    public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
	    }

	   
	  

	 
	  protected   String bitmapToBase64(Bitmap bitmap) {  
		  
		    String result = null;  
		    ByteArrayOutputStream baos = null;  
		    try {  
		        if (bitmap != null) {  
		            baos = new ByteArrayOutputStream();  
		            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
		  
		            baos.flush();  
		            baos.close();  
		  
		            byte[] bitmapBytes = baos.toByteArray();  
		            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);  
		        }  
		    } catch (IOException e) {  
		        e.printStackTrace();  
		    } finally {  
		        try {  
		            if (baos != null) {  
		                baos.flush();  
		                baos.close();  
		            }  
		        } catch (IOException e) {  
		            e.printStackTrace();  
		        }  
		    }  
		    return result;  
		}
	  
	 
		@Override
		protected void onCreateCompeleted(View arg0) {
			// TODO Auto-generated method stub
			  File outputImage = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
              CropOptions options=  new CropOptions.Builder().create();
		      getTakePhoto().onPickFromCaptureWithCrop(Uri.fromFile(outputImage), options);
		}

		@Override
		public int getLayoutViewId() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int getModeCode() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public boolean initView(View parentView) {
			// TODO Auto-generated method stub
			return false;
		}
 
}
